package com.example.controller;


import com.example.entity.Adminuser;
import com.example.service.AdminuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
public class LogibCtrl {

    @Autowired
//    @Resource
    AdminuserService adminuserService;

    @GetMapping("/toLoginPage")
    public String aa() {
        return "first";
    }


    @RequestMapping("/dologin")
//    @PostMapping("/DengLu")
    public String bbb(Adminuser adminuser, Model model,HttpSession session) {
        System.out.println(DigestUtils.md5DigestAsHex(adminuser.getAdupwd().getBytes()));
        adminuser.setAdupwd( DigestUtils.md5DigestAsHex(adminuser.getAdupwd().getBytes()));

//        System.out.println("进行登录处理" + adminuser.getAduname() + "--" + adminuser.getAdupwd());
        Adminuser admin = adminuserService.findAdmin(adminuser);
        if ( admin== null) {
            model.addAttribute("msg", "用户名或者密码错误！");
            return "first";
        } else {
            System.out.println(admin);
            session.setAttribute("aduName",admin.getAduname());
            return "redirect:/admin/goods";

        }
    }
    @GetMapping("/logout")
    public String doLogout(HttpSession session, SessionStatus sessionStatus){
        // 只是让session失效 但session仍然存在
        session.invalidate();
        sessionStatus.setComplete();
        return "first";
    }
}



